package io.gitee.hechenl.fastiotreport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.gitee.hechenl.fastiotreport.domain.model.ConfigPoint;
import io.gitee.hechenl.fastiotreport.mapper.ConfigPointMapper;
import io.gitee.hechenl.fastiotreport.service.IConfigPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Service
public class ConfigPointServiceImpl implements IConfigPointService {

    @Autowired
    private ConfigPointMapper configPointMapper;

    @Override
    public void insert(List<ConfigPoint> list) {
        list.forEach(v -> configPointMapper.insert(v));
    }

    @Override
    public List<ConfigPoint> list(String pointId, String pointName) {
        LambdaQueryWrapper<ConfigPoint> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(pointId)) {
            wrapper.like(ConfigPoint::getPointId, pointId.trim());
        }
        if (StringUtils.hasText(pointName)) {
            wrapper.like(ConfigPoint::getPointName, pointName.trim());
        }
        return configPointMapper.selectList(wrapper);
    }

    @Override
    public ConfigPoint list(String pointId) {
        List<ConfigPoint> list = list(Collections.singletonList(pointId));
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public List<ConfigPoint> list(List<String> pointIds) {
        LambdaQueryWrapper<ConfigPoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ConfigPoint::getPointId, pointIds);
        return configPointMapper.selectList(wrapper);
    }
}
